home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
- * $Id: tmpf.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
- *
- *. Copyright(c) 1993,1994 by T.C. Zhao
- * All rights reserved.
- *.
- * Temprory files.
- ***********************************************************************/
-
- #if !defined(lint) && defined(F_ID)
- char *id_tmpf = "$Id: tmpf.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
- #endif
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <malloc.h>
- #include "ulib.h"
-
- #if defined(__STDC__) || defined(__STRICT_ANSI__)
- extern char *tempnam(const char *, const char *);
- #endif
-
- #define MAXTMPF 50
- static char *fname[MAXTMPF];
-
- /******* search the table for a name ******/
- static char **
- find_slot(register const char *s)
- {
- register char **p = fname, **ps;
-
- for (ps = p + MAXTMPF; p < ps; p++)
- if (*p == s)
- return p;
- fputs("tmpf: can't find avialable space\n", stderr);
- /* overwrite the first one if is a request */
- return s ? 0 : fname;
- }
-
- /********** get a tmp file name **************/
- char *
- get_tmpf(const char *pref)
- {
- char *p;
-
- /* It appears that SGI's tempnam has a bug that tempnam sets the errno */
- int oerrno = errno;
- /* find slot always returns a valid one */
- p = *(find_slot(0)) = tempnam(0, pref);
- errno = oerrno;
- return p;
- }
-
- /******* delete the tmp file ******************/
- void
- del_tmpf(char *name)
- {
- char **p;
-
- if (!name || !(p = find_slot(name)))
- {
- fputs("del_tmpf: bad argument\n", stderr);
- return;
- }
-
- if (access(name, F_OK) == 0 && remove(name) == 0)
- {
- free(name);
- *p = 0;
- }
- }
-
- /******** remove all tmp files used. **********/
- void
- del_all_tmpf(void)
- {
- char **p = fname, **ps;
-
- for (ps = p + MAXTMPF; p < ps; p++)
- {
- if (*p)
- {
- remove(*p);
- free(*p);
- *p = 0;
- }
- }
- }
-